<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['device_id'])) {
	header("HTTP/1.1 400 Bad Request");
	exit;
}

require_once 'db.inc.php';

$user_id_sql = "SELECT user FROM ".TABLE_PREFIX."devices WHERE deviceId = '".$_POST['device_id']."'";
$result = mysql_query("SELECT * FROM ".TABLE_PREFIX."devices WHERE user = ($user_id_sql)") or die(mysql_error());
if (mysql_num_rows($result) > 1) {
	mysql_query("UPDATE ".TABLE_PREFIX."devices SET user = NULL WHERE deviceId = '".$_POST['device_id']."'") or die(mysql_error());
} else {
	mysql_query("DELETE FROM ".TABLE_PREFIX."shifts WHERE user = ($user_id_sql);") or die(mysql_error());
	mysql_query("DELETE FROM ".TABLE_PREFIX."users WHERE id = ($user_id_sql);") or die(mysql_error());
	mysql_query("UPDATE ".TABLE_PREFIX."devices SET user = NULL WHERE deviceId = '".$_POST['device_id']."'") or die(mysql_error());
}
header("HTTP/1.1 204 No Content");

?>